;layui.define(function(e) {
    layui.use(['table','layer','form','laydate','util'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start'
        });
        laydate.render({
            elem: '#end'
        });

        //展示已知数据
        table.render({
            elem: '#customerList'
            ,id: 'listReload'
            ,url: hostUrl+'/clue/storeSaleAccCluePoolList?dealerId='+globalDealerId+"&storeId="+globalStoreId+"&clueisorderstatu="+0
            ,loading: true
            ,cols: [[ //标题栏
            	{type:'radio', fixed: 'left' }
                ,{field: 'name', title: '客户姓名'}
                ,{field: 'sex', title: '性别',templet: function(d){
                	if(d.sex == '0') {
	                    return '男';
                	} else if(d.sex == '1') {
	                    return '女';
                	} else {
	                    return '-';
                	}
                }}
                ,{field: 'phone', title: '手机号'}
                ,{field: 'type', title: '客户类型',templet: function(d){
                	if(d.type == '1'){
	                    return '个人客户';
                	}else if(d.type == '2'){
	                    return '企业客户';
                	}else{
	                    return '-';
                	}
                }}
                ,{field: 'clue_o_level', title: '客户等级',templet: function(d){
                	if(d.clue_o_level == null || d.clue_o_level == 'null' || d.clue_o_level == ''){
                		if(d.clue_level == null || d.clue_level == 'null' || d.clue_level == ''){
                			return "-";
                		}else{
                			return d.clue_level;
                		}
                	}else{
                		if(d.clue_o_level == null || d.clue_o_level == 'null' || d.clue_o_level == ''){
                			return "-";
                		}else{
                			return d.clue_o_level;
                		}
                	}
                }}
            ]]
            ,page: true //是否显示分页
        });
       
        form.on('select(selectStore)', function(data){
        	selectStore();
        });

        // 新建客户
        $("#newCustomers").on("click", function () {
        	 var index = layer.open({
                 type: 2
                 ,id: (new Date()).valueOf()
                 ,title: '新建线索'
                 ,content: hostUrl+'/clue/newClue/storeSaleNewClue.do'
                 ,area: ['500px', '500px']
             });
             layer.full(index);
        })

        //表刷新方法
        var reloadTable = function () {
            table.reload("listReload", { //此处是上文提到的 初始化标识id
                where: {
                    name: $("#name").val(),
                    phone: $("#phone").val()
                   
                }
            });
        };

        // 搜索按钮
        $("#search-button").on("click",function () {
            reloadTable();
        });

        var objList;
        //关联车辆页面中:获取单选框的数据
        table.on('radio(listTable)', function(obj){
            objList = obj.data;
        });

        //关联客戶页面中：点击保存按钮
        $('#glkh-btnsave').on("click", function () {
            // objList
            // 给父页面赋值
            parent.$("#name").val($.common.isEmpty(objList.name) ? '' : objList.name);
            parent.$("#sex").val($.common.isEmpty(objList.sex) ? '' : String(objList.sex).replace("1",'女').replace('0','男'));
            parent.$("#phone").val($.common.isEmpty(objList.phone) ? '' : objList.phone);
            //parent.$("#clue_source_type").val(objList.clue_source_type);
            parent.$("#job_company").val($.common.isEmpty(objList.job_company) ? "" : objList.job_company);
            parent.$("#job_position").val($.common.isEmpty(objList.job_position)? "" : objList.job_position) ;
            parent.$("#we_chat").val($.common.isEmpty(objList.we_chat) ? "" : objList.we_chat);
            parent.$("#is_marry").val($.common.isEmpty(objList.is_marry) ? "" : String(objList.is_marry).replace('0','未婚').replace("1",'已婚'));
            parent.$("#type").val($.common.isEmpty(objList.type) ? "" : String(objList.type).replace('1','个人').replace("2",'企业'));
            parent.$("#customerId").val($.common.isEmpty(objList.clue_id) ? "" : objList.clue_id);
            parent.$("#clue_o_level").val($.common.isEmpty(objList.clue_o_level) ? objList.clue_level : objList.clue_o_level);
            // 祥辉ID
            parent.$("#zhId").text($.common.isEmpty(objList.id) ? '' : objList.id);
            //parent.$("#clue_o_level").val(objList.clue_o_level);
            // 显示父页面客户信息
            parent.$("#hiddenInput").attr("class","");
            closeWindow();
        });

        // 关闭当前弹出窗口
        function closeWindow() {
            //先得到当前iframe层的索引
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index); //再执行关闭
        }
    });
});